<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * base_url/church-locator
 *
 * @package    controllers
 * @author     Bertrand Kintanar <bertrand.kintanar@gmail.com>
 * @version    $Id: church_locator.php 214 2011-12-08 21:01:26Z bertrand.kintanar@gmail.com $
 * @copyright  &copy; 2011 NaCl.org WebGroup
 *
 */
class Church_locator extends MY_Controller {

    private $_states_with_coords = array(
            'Bohol'          => '9.83,124.26', 
            'Biliran'        => '11.61,124.47', 
            'Cebu'           => '10.40,123.89', 
            'Leyte'          => '10.68,124.95', 
            'Southern Leyte' => '10.37,125.08', 
            'Eastern Samar'  => '11.38,125.48', 
            'Northern Samar' => '12.39,124.78', 
            'Samar'          => '11.96,125.04');
    
    public function __construct()
    {
        parent::__construct();
        parent::_get_script(__FILE__);
    }

    public function index()
    {
        $_state_obj = new \models\State;
        $states     = $_state_obj->getStateByNames(array_keys($this->_states_with_coords));
        
        $this->smarty->assign('states', $states);

		$this->smarty->assign('load_modal' , true);
		$this->smarty->assign('load_map'   , true);
		
        $this->smarty->view();
    }
    
    public function section()
    {
        $_section_id = $this->uri->segment(3);

        $_section_obj = new \models\Section;
        $_section = $_section_obj->getSectionById($_section_id);
        
        if ($_section)
        {
            $_church_obj = new \models\Church;
            $_churches = $_church_obj->getChurchesBySectionId($_section_id);
            
            print ('List of Churches in Section ' . $_section_id . ': ' . $_section->section_name . '<br><br>');
            
            foreach($_churches as $church)
            {
                $url = $church->churchFullNameURL();
                print ("<a href='$url'>" . $church->church_full_name . "</a><br>");
            }
        }
        else
        {
            print('section not valid');
        }
        
        $this->smarty->assign('main_content', 'default.tpl');
        $this->smarty->view();
    }
    
    public function search()
    {
        // http://base_url/church-locator/$state_id-$state_name/$city_id-$city_name
        
        $_state_id = (int) $this->uri->segment(3);
        $_city_id  = (int) $this->uri->segment(4);
        
        $_church_obj = new \models\Church;
        
        $_churches = $_church_obj->getChurchesBySearchQuery($_state_id, $_city_id);
        
        foreach($_churches as $_church)
        {
               $_pastor_profile_obj = new \models\PastorProfile;
            $_pastor_profile = $_pastor_profile_obj->getPastorProfileByChurchId($_church->church_id);
            
            print('Church Name: ' . $_church->church_name . '<br>');
            print('Church Full Name: ' . $_church->church_full_name . '<br>');
            print('Section: ' . $_church->section->section_name . '<br>');
            print('Pastor: ' . $_pastor_profile->getPastorProfileNameWithTitle() . '<br>');
            print('Address: ' . $_church->address->display() . '<br>');
            
            print("<br>");
        }
        
        $this->smarty->assign('main_content', 'default.tpl');
        $this->smarty->view();
    }
  
    public function church()
    {
        $_church_id = $this->uri->segment(3);
        
        $_church_obj = new \models\Church;
        $_church = $_church_obj->getChurchById($_church_id);
        
        if ($_church)
        {
               $_pastor_profile_obj = new \models\PastorProfile;
            $_pastor_profile = $_pastor_profile_obj->getPastorProfileByChurchId($_church_id);
            
            print('Church Name: ' . $_church->church_name . '<br>');
            print('Church Full Name: ' . $_church->church_full_name . '<br>');
            print('Section: ' . $_church->section->section_name . '<br>');
            print('Pastor: ' . $_pastor_profile->getPastorProfileNameWithTitle() . '<br>');
            print('Address: ' . $_church->address->display() . '<br>');
        }  
        else
        {
            print('church not valid');
        }
        
        $this->smarty->assign('main_content', 'default.tpl');
        $this->smarty->view();
    }
    
    function update_cities()
    {
        $_state_id = $this->input->post('state_id');
        
        $_city_obj = new \models\City;
        $_state_obj = new \models\State;
        
        $state = $_state_obj->getStateById($_state_id);
        
        $coords = $this->_states_with_coords[$state->state_name];
        
        $cities = $_city_obj->getCitiesByStateId($_state_id);
        
        $json = array();
        $json[] = $coords;
        foreach($cities as $city)
        {
            $temp['id']   = $city->city_id;
            $temp['name'] = $city->city_name;
            
            $json[] = $temp;
        }
        
        print(json_encode($json));
    }
    
    function update_search_result()
    {
        $per_page  = 10;
        
        $_state_id = $this->input->post('state_id');
        $_city_id  = $this->input->post('city_id');
        $_query    = $this->input->post('query');
        $_offset   = $this->input->post('offset') ? $this->input->post('offset') : 0;
        
        if ($_query == 'Search Church...')
        {
            $_query = '';
        }
        
        $_church_obj         = new \models\Church;
        $_section_obj        = new \models\Section;
        $_social_obj         = new \models\Social;
        $_pastor_profile_obj = new \models\PastorProfile;
        
        $_churches = $_church_obj->getChurchesBySearchQuery($_state_id, $_city_id, $_query, $_offset);
        $json = array();
        foreach( $_churches as $_church )
        {
            $_church_id           = $_church->church_id;
            $_pastor_profile      = $_pastor_profile_obj->getPastorProfileByChurchId($_church_id);
            $_section             = $_section_obj->getSectionById($_church->section_id);
            $_socials             = $_social_obj->getSocialsByChurchId($_church_id);
           
            $church['address']    = htmlentities($_church->address->display());
            $church['pastor']     = htmlentities($_pastor_profile->getPastorProfileNameWithTitle());
            $church['link']       = $_church->getChurchFullNameURL();
            $church['founded']    = $_church->church_founded ? $_church->church_founded->format('F Y') : '-';
            $church['church_obj'] = $_church;
            $church['pastor_obj'] = $_pastor_profile;
            $church['social_obj'] = $_socials;
            
            
            $json[] = $church;
            
            if ( $i + 1 == $per_page )
            {
                break;
            }
            $i++;
        }
        print( json_encode( $json ) );
    }
}
